package com.dcy.file.biz.config.file;

import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.DownloadFileRequest;
import com.aliyun.oss.model.DownloadFileResult;
import com.aliyun.oss.model.OSSObject;
import com.dcy.file.biz.model.FileInfo;
import com.dcy.file.biz.properties.FileServerProperties;
import com.dcy.file.biz.service.IFileHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @Author：dcy
 * @Description: 阿里云配置
 * @Date: 2019/9/18 13:51
 */
@Configuration
@ConditionalOnProperty(name = "dcy.file-server.type", havingValue = "aliyun")
public class AliyunOSSAutoConfigure {

    @Autowired
    private FileServerProperties fileProperties;

    /**
     * 阿里云配置
     *
     * @return
     */
    @Bean
    public OSSClient ossClient() {
        return new OSSClient(fileProperties.getOss().getEndpoint()
                , new DefaultCredentialProvider(fileProperties.getOss().getAccessKey(), fileProperties.getOss().getAccessKeySecret())
                , new ClientConfiguration());
    }

    @Service
    public class AliyunOssServiceImpl implements IFileHandler {
        @Autowired
        private OSSClient ossClient;

        @Override
        public String fileType() {
            return fileProperties.getType();
        }

        @Override
        public void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception {
            this.uploadFile(file.getInputStream(), fileInfo);
        }

        @Override
        public void uploadFile(InputStream inputStream, FileInfo fileInfo) throws Exception {
            ossClient.putObject(fileProperties.getOss().getBucketName(), fileInfo.getName(), inputStream);
            fileInfo.setUrl(fileProperties.getOss().getDomain() + "/" + fileInfo.getName());
        }

        @Override
        public boolean deleteFile(FileInfo fileInfo) {
            ossClient.deleteObject(fileProperties.getOss().getBucketName(), fileInfo.getName());
            return true;
        }

        @Override
        public void downLoadFile(String fileName, String filePath, HttpServletResponse response) {
            try {
                OSSObject object = ossClient.getObject(fileProperties.getOss().getBucketName(), filePath);
                ServletUtil.write(response,object.getObjectContent(), MediaType.APPLICATION_OCTET_STREAM_VALUE,fileName);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }

}
